﻿using CommonBase;
using EnvCommonUtility;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using SurveillanceLib;
using SurveillancePro.Base.Events;
using SurveillancePro.Base.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;

namespace PTZControlModule.ViewModels
{
    /// <summary>
    /// 云台控制
    /// </summary>
    public class ViewModel_PTZControl : WorkspaceViewModel
    {
        #region Private Fields

        private DelegateCommand<string> _cmdPTZControlMouseDown, _cmdPTZControlMouseUp;

        #endregion

        #region Constructor

        public ViewModel_PTZControl(IEventAggregator eventAggregator)
        {
            _canPTZControl = Visibility.Collapsed; // 不可用
            _fillStepsList(); // 加载 Steps 下拉框

            #region 模块间通信
            // 接收主模块的消息(EnvPanelOperation | EnvUtilities)
            OpenModelEvent openEvent = eventAggregator.GetEvent<OpenModelEvent>();
            openEvent.Subscribe(_manageData, ThreadOption.UIThread, true,
                x => x.ModelName == this.GetType().Name);
            #endregion
        }

        #endregion

        #region Base Class Overrides

        protected override void OnDispose()
        {
            _dispose();
        }

        #endregion

        #region Public Property

        /// <summary>
        /// 步长列表
        /// </summary>
        public List<ushort> Steps { get; private set; }

        /// <summary>
        /// 步长
        /// </summary>
        public ushort Step { get; set; }

        private Visibility _canPTZControl;
        /// <summary>
        /// 可以进行云台控制
        /// </summary>
        public Visibility CanPTZControl
        {
            get { return _canPTZControl; }
            set
            {
                if (_canPTZControl == value) return;
                _canPTZControl = value;
                base.OnPropertyChanged("CanPTZControl");

                SurveillancePro.Base.Reflector.WorkingStateProperty.OnWorkingStatePropertyChanged<string>(this); // 更新界面
            }
        }


        /// <summary>
        /// 按钮按下时
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand<string> Cmd_PTZControlMouseDown
        {
            get
            {
                if (_cmdPTZControlMouseDown == null)
                {
                    _cmdPTZControlMouseDown = new DelegateCommand<string>(_ptzControlMouseDown, 
                        _canPTZControlMouseDown);
                }
                return _cmdPTZControlMouseDown;
            }
        }

        /// <summary>
        /// 按钮松开时
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand<string> Cmd_PTZControlMouseUp
        {
            get
            {
                if (_cmdPTZControlMouseUp == null)
                {
                    _cmdPTZControlMouseUp = new DelegateCommand<string>(_ptzControlMouseUp, 
                        _canPTZControlMouseUp);
                }
                return _cmdPTZControlMouseUp;
            }
        }

        #endregion

        #region Private Methods

        #region DelegateCommand

        private bool _canPTZControlMouseDown(string direction)
        {
            return this._canPTZControl == Visibility.Visible;
        }

        private bool _canPTZControlMouseUp(string direction)
        {
            return this._canPTZControl == Visibility.Visible;
        }


        /// <summary>
        /// 按钮按下时
        /// </summary>
        /// <param name="direction">方向</param>
        private void _ptzControlMouseDown(string direction)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(direction) &&
                    EnvPanelOperation.SelectedPanel != null &&
                    EnvPanelOperation.SelectedPanel.Tag != null &&
                    (EnvPanelOperation.SelectedPanel.Tag is TagModel) &&
                    (EnvPanelOperation.SelectedPanel.Tag as TagModel).Driver_Instance != null &&
                    !string.IsNullOrWhiteSpace((EnvPanelOperation.SelectedPanel.Tag as TagModel).Driver_LoginID) &&
                    !string.IsNullOrWhiteSpace((EnvPanelOperation.SelectedPanel.Tag as TagModel).Panel_SelectedChannel))
                {
                    TagModel panelTag = EnvPanelOperation.SelectedPanel.Tag as TagModel;
                    if(panelTag.Driver_Instance.PTZControl(panelTag.Driver_LoginID, 
                        int.Parse(panelTag.Panel_SelectedChannel),
                        direction, Step, false)) // 云台控制
                    {
#if DEBUG
                        Debug.WriteLine(string.Format("Action: {0}", direction));
#endif
                    }
                    else
                    {
#if DEBUG
                        Debug.WriteLine(string.Format("Action Failed: {0}", direction));
#endif
                    }
                }
                else
                {
                    MessageBox.Show("视频窗口未选择，或不能操作该视频窗口", ConstantString.MSG_COMPANY);
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        /// <summary>
        /// 按钮松开时
        /// </summary>
        /// <param name="direction">方向</param>
        private void _ptzControlMouseUp(string direction)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(direction) &&
                    EnvPanelOperation.SelectedPanel != null &&
                    EnvPanelOperation.SelectedPanel.Tag != null &&
                    (EnvPanelOperation.SelectedPanel.Tag is TagModel) &&
                    (EnvPanelOperation.SelectedPanel.Tag as TagModel).Driver_Instance != null &&
                    !string.IsNullOrWhiteSpace((EnvPanelOperation.SelectedPanel.Tag as TagModel).Driver_LoginID) &&
                    !string.IsNullOrWhiteSpace((EnvPanelOperation.SelectedPanel.Tag as TagModel).Panel_SelectedChannel))
                {
                    TagModel panelTag = EnvPanelOperation.SelectedPanel.Tag as TagModel;
                    if(panelTag.Driver_Instance.PTZControl(panelTag.Driver_LoginID, 
                        int.Parse(panelTag.Panel_SelectedChannel),
                        direction, Step, true)) // 云台控制
                    {
#if DEBUG
                        Debug.WriteLine(string.Format("Action: {0}", direction));
#endif
                    }
                    else
                    {
#if DEBUG
                        Debug.WriteLine(string.Format("Action Failed: {0}", direction));
#endif
                    }
                }
                else
                {
                    MessageBox.Show("视频窗口未选择，或不能操作该视频窗口", ConstantString.MSG_COMPANY);
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        #endregion

        #region 主模块传入执行的方法
        /// <summary>
        /// 事件入口
        /// </summary>
        /// <param name="arg"></param>
        private void _manageData(OpenModelEventArg arg)
        {
            if (string.IsNullOrWhiteSpace(arg.FunctionName))
            {
#if DEBUG
                Debug.WriteLine("无效的执行操作，请输入方法名称！");
#endif
                return;
            }

            try
            {
                switch (arg.FunctionName)
                {
                    case "ChangePTZControlState": // 修改云台控制按钮状态
                        if (arg.Arg != null && arg.Arg is Visibility)
                        {
                            _changePTZControlState((Visibility)arg.Arg);
                        }
                        else
                        {
#if DEBUG
                            Debug.WriteLine("传入的云台按钮状态有问题！");
#endif
                            MessageBox.Show("修改云台控制按钮状态异常！", ConstantString.MSG_COMPANY);
                        }
                        break;


                    default:
                        break;
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
            }
        }
        #endregion

        /// <summary>
        /// 填充步长下拉值
        /// </summary>
        private void _fillStepsList()
        {
            Steps = new List<ushort>();
            for (ushort i = 1; i <= 10; i++)
            {
                Steps.Add(i);
            }
            Step = 4;
        }

        /// <summary>
        /// 修改云台控制按钮状态
        /// </summary>
        /// <param name="isShow"></param>
        private void _changePTZControlState(Visibility isShow)
        {
            CanPTZControl = isShow;
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        private void _dispose()
        {
            try
            {
                if (Steps != null)
                    Steps.Clear();
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        #endregion
    }
}
